package com.jet.chat.dbquery;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ChatQueryIsUserExist extends ChatQueryAbstract {

	@Override
	protected ChatQueryResult main(Connection conn, ChatQueryParams chatQueryParams) throws Exception {
		String username = ((ChatQueryIsUserExistParams) chatQueryParams).getUsername();
		
		boolean result = false;
		
		try {
			PreparedStatement ps = conn.prepareStatement("select id from users where username = ?");
			ps.setString(1, username);
			ResultSet results = ps.executeQuery();
			while(results.next()) {
				result = true;
			}
			results.close();
		} catch (SQLException e) {
			throw new Exception(e);
		}

		return new ChatQueryIsUserExistResult(result);
	}

}
